package com.example.aggregatedemo.arrary;

import com.example.aggregatedemo.arrary.a05mySet.Student;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashSet;

@SpringBootTest
public class HashSetTest_1 {

    @Test
    void DemoTest1(){

        //创建对象
        Student s1=new Student("z113",21);
        Student s2=new Student("z113",21);
        //如果没重写hashCode，不同对象计算的hashCode值是不一样的。
        System.out.println(s1.hashCode());//1979455890
        System.out.println(s2.hashCode());//1605128733


        //小部分情况，不同属性值或地址值计算哈希值可能一样。（哈希碰撞）
        System.out.println("abc".hashCode());
        System.out.println("acD".hashCode());
    }

    @Test
    void DemoTest2(){
        //对象成员值相同，我们认为是一个队形
        Student s1=new Student("z113",21);
        Student s2=new Student("z113",21);
        Student s3=new Student("w5",21);
        Student s4=new Student("z6",23);

        HashSet<Student> hs = new HashSet<>();
        System.out.println(hs.add(s1));
        System.out.println(hs.add(s2));
        System.out.println(hs.add(s3));
        System.out.println(hs.add(s4));

        System.out.println(hs);
    }

}
